
local require  = require;
local ngx      = ngx;
local debug    = debug;
local string   = string;
local tonumber = tonumber;
local setmetatable = setmetatable
local pairs    = pairs
local type     = type
local table    = table

local math       = math
local math_floor = math.floor

----数据变量----
local uu         = require("modules.commhm.utils");
local log        = require("modules.commhm.log");
local ns_time    = require("modules.commhm.time");
local huu        = require("modules.html_page.html_utils")

-- 玩家的房间类
-- _VERSION = '0.12.1'
local _M = {}
local mt = { __index = _M }


function _M.new(self, t_ )
    log.debug( "call CHtmlPage:new");
    local ins_ = {
        data = t_,
        print_list = {},
        print_str  = nil,
    };

    local ret_ = setmetatable(ins_, mt);
    return ret_
end


---计算一个简单结构的 table, 包含在内的key之内的才会打印
---{
--- keys={ "a", "b", "c", "d", "e" },
---{ a=b, b=c, c=d, d=a }
---{ c=c, b=c, a=c, dd=d }
--- style={ 'width="100px"' }
--- data_style={ 'width="100px"' }
---}

function _M.get_print_string(self)
    if  self.print_str then
        return  self.print_str
    end

    self:add( "<table border=1 cellpadding=5>\n" )
    if  self.data then
        if  self.data.keys  then
            self:add_print_tr( self.data.keys, self.data.style )
            for i=1, #self.data do
                local tmp_ = {}
                for k=1, #self.data.keys do
                    tmp_[ #tmp_ + 1 ] = self.data[i][ self.data.keys[k] ] or ''
                end
                self:add_print_tr( tmp_, self.data.data_style )
            end
        end
    end
    self:add( "</table>\n" );

    ---uu.var_dump( self.print_list )

    ---计算整个打印内容
    self.print_str = table.concat( self.print_list )
    return  self.print_str
end


-------------------------------utils----------------------------

---增加一个打印内容
function _M.add(self, content_)
    ---log.debug( "add==== [" .. content_ .. "]" )
    self.print_list[#self.print_list + 1] = content_
end


---增加打印一行
function _M.add_print_tr(self, tr_, style)
    --log.debug( "add tr==== [" .. #tr_ .. "]" )

    self:add( '<tr>' )
    for i=1, #tr_ do
        if  style and style[i] and #style[i]>0 then
            self:add( '<td ' ..  style[i] ..  '>' )
        else
            self:add( '<td>' )
        end
        self:add( tr_[i] )
        self:add( '</td>' )
    end
    self:add( '</tr>\n' )
end


return _M;
